<?php
include('ip2locationlite.class.php');
 

//$current_domain= $_SERVER['HTTP_HOST'];
$current_domain="home24.com";
$host_names = explode(".", $current_domain);
$domain = $host_names[0];
//echo "domain=$domain.<br>";

//echo "current domain is $current_domain";  //get the current domain of user

if(!(strcmp($current_domain,"home24.com"))){    //check if its browsing home24
	$ip = gethostbyname($current_domain);       //gethostname for URL
	if(!(strcmp($current_domain,$ip))) {       //gethostbyname is NOT SUCCESSFUL
		echo "DNS Lookup failed";
	}
	else {                     //if gethostbyname is SUCCESSFUL
		/*if (!cookie)
		     run API
		else 
		    useCookie;
		*/
		$cookie = (isset($_COOKIE['geolocation'])) ? $_COOKIE['geolocation'] : false;
		if(!$_COOKIE["geolocation"]){     //check if COOKIE exists
			//cookie does not exists
			//Load the class
			$ipLite = new ip2location_lite;               
			$ipLite->setKey('9106ff06cf24e0a0ce6d190cd73b5b0758e5a6f5e5adae60ff6d79c63d3a07d8');   //Input the API key, obtained after registering on IPinfoDB
			//Get errors and locations
			$locations = $ipLite->getCountry($_SERVER['REMOTE_ADDR']);  // getCountry info 
			//$locations = $ipLite->getCity($_SERVER['REMOTE_ADDR']);   //Uncomment to getCity instead of getCountry
				
			 if ($visitorGeolocation['statusCode'] == 'OK') {   //create new cookie
				$data = base64_encode(serialize($visitorGeolocation));
				$dom = ($_SERVER['HTTP_HOST'] != 'localhost') ? $_SERVER['HTTP_HOST'] : false;
				echo "dom=$dom.<br>";
				setcookie("geolocation", $data, time()+3600*24*7*2, '/', $dom, false); //set cookie for 2 weeks
			  }
			  //run API 
			$errors = $ipLite->getError();
 				//Getting the result
				echo "<p>\n";
				echo "<strong>First result</strong><br />\n";

				if (!empty($locations) && is_array($locations)) {   
				  foreach ($locations as $field => $val) {
					echo $field . ' : ' . $val . "<br />\n";   //Display 

					if(!(strcmp($field,"countryCode"))){    
				   $countryC=$val;							//Extract countryCode from API
					}
				  }
				}
				//checking for various country code using Switch Case
				switch($countryC){
					case 'SG':
						//$r="http://www.".$domain.".".$countryC;
						$r="http://www.".$domain.".".$countryC;
						header( "Location:".$r ) ;              //browser redirection 
						print $r;
						break;
					case 'MY':
						$r="<a href=\"http://www.".$domain.".".$countryC."\">";
						echo $r;
						break;
					case 'VN':
						$r="<a href=\"http://www.".$domain.".".$countryC."\">";
						echo $r;
						break;
					case 'DE':
						$r="<a href=\"http://www.".$domain.".".$countryC."\">";
						echo $r;
						break;
					case 'ID':
						$r="<a href=\"http://www.".$domain.".".$countryC."\">";
						echo $r;
						break;
					case 'IN':
						$r="<a href=\"http://www.".$domain.".".$countryC."\">";
						echo $r;
						break;
					default:
						echo "error";
				}
				//print "\n***The countryCode for given IP address is =$countryC***\n";          
				echo "</p>\n";

							}
				else{			//if  already exists
					$visitorGeolocation = unserialize(base64_decode($_COOKIE["geolocation"]));
					}
				 var_dump($visitorGeolocation);  //Dumping cookie content

					}
				}
				else {
					echo "API is not running";
					//DO NOT run API
				 }

//Check if cookie is created
echo "cookie";
$cookie = (isset($_COOKIE['cookie'])) ? $_COOKIE['cookie'] : false;
print_r ($_cookie);
				 
//Show errors
echo "<p>\n";
echo "<strong>Dump of all errors</strong><br />\n";
if (!empty($errors) && is_array($errors)) {
  foreach ($errors as $error) {
    echo var_dump($error) . "<br /><br />\n";
  }
} else {
  echo "No errors" . "<br />\n";
}
echo "</p>\n";



/*
$domain = 'www.blah.foo';
$ip = gethostbyname($domain);
if($domain != $ip) {
    // do something
	echo "DNS Lookup failed";
	}
	
//PARSING A URL
$url = 'http://google.com/dhasjkdas/sadsdds/sdda/sdads.html';
$parse = parse_url($url);
print $parse['host']; // prints 'google.com'


//CONCATENATION 
<?php
$domain ="home24";
$countryC="sg";

$h="http://www";

$r="$domain.".".$countryC;
echo $r;
?>
*/